/************************************************************
 *  * Hyphenate CONFIDENTIAL
 * __________________
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of Hyphenate Inc.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Hyphenate Inc.
 */

#import "AppDelegate+EaseMob.h"
//#import "ApplyViewController.h"
#import "LoginViewController.h"
#import "ChatDemoHelper.h"
#import "MBProgressHUD.h"
#import "CustomNavViewController.h"

/**
 *  本类中做了EaseMob初始化和推送等操作
 */

@implementation AppDelegate (EaseMob)

- (void)easemobApplication:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
                    appkey:(NSString *)appkey
              apnsCertName:(NSString *)apnsCertName
               otherConfig:(NSDictionary *)otherConfig
{
    //注册登录状态监听
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginStateChange:)
                                                 name:KNOTIFICATION_LOGINCHANGE
                                               object:nil];
    
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    BOOL isHttpsOnly = [ud boolForKey:@"identifier_httpsonly"];
    
    [[EaseSDKHelper shareHelper] hyphenateApplication:application
                    didFinishLaunchingWithOptions:launchOptions
                                           appkey:appkey
                                     apnsCertName:apnsCertName
                                      otherConfig:nil];
    
    [ChatDemoHelper shareHelper];
    
    BOOL isAutoLogin = [EMClient sharedClient].isAutoLogin;
    if (isAutoLogin){
        [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@YES];
    }
    else
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
    }
}

- (void)easemobApplication:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [[EaseSDKHelper shareHelper] hyphenateApplication:application didReceiveRemoteNotification:userInfo];
}

#pragma mark - App Delegate

// 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [[EMClient sharedClient] bindDeviceToken:deviceToken];
//        [JPUSHService registerDeviceToken:deviceToken];
    });
}

// 注册deviceToken失败，此处失败，与环信SDK无关，一般是您的环境配置或者证书配置有误
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{

}

#pragma mark - login changed

- (void)loginStateChange:(NSNotification *)notification
{
    BOOL loginSuccess = [notification.object boolValue];
    if (!loginSuccess) {
        //已退出
        [[NSNotificationCenter defaultCenter]postNotificationName:kLogOutNotification object:nil];
    }
}
//{
//    BOOL loginSuccess = [notification.object boolValue];
////    EMNavigationController *navigationController = nil;
//    if (loginSuccess) {//登陆成功加载主窗口控制器
//        //加载申请通知的数据
////        [[ApplyViewController shareController] loadDataSourceFromLocalDB];
////        if (self.mainController == nil) {
////            self.mainController = [[MainViewController alloc] init];
////            navigationController = [[EMNavigationController alloc] initWithRootViewController:self.mainController];
////        }else{
////            navigationController  = (EMNavigationController *)self.mainController.navigationController;
////        }
////        // 环信UIdemo中有用到Parse，您的项目中不需要添加，可忽略此处
////        [self initParse];
//        
//        [ChatDemoHelper shareHelper].mainVC = self.rootTabBarController;
////        [self.rootTabBarController.myVC updateData];
//        [[ChatDemoHelper shareHelper] asyncGroupFromServer];
//        [[ChatDemoHelper shareHelper] asyncConversationFromDB];
//        [[ChatDemoHelper shareHelper] asyncPushOptions];
//    }
//    else{//登陆失败加载登陆页面控制器
//        if (self.rootTabBarController) {
//            [self.rootTabBarController.navigationController popToRootViewControllerAnimated:NO];
//            self.rootTabBarController.selectedIndex = 0;
//        }
//        [ChatDemoHelper shareHelper].mainVC = nil;
////        [XFUser userLogOut];
//        LoginViewController *vc = [LoginViewController new];
//        CustomNavViewController *newNav = [[CustomNavViewController alloc] initWithRootViewController:vc];
//        [self.rootTabBarController presentViewController:newNav animated:YES completion:nil];
//    }
//    
//    //设置7.0以下的导航栏
//    if ([UIDevice currentDevice].systemVersion.floatValue < 7.0){
////        navigationController.navigationBar.barStyle = UIBarStyleDefault;
////        [navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"titleBar"]
////                                                 forBarMetrics:UIBarMetricsDefault];
////        [navigationController.navigationBar.layer setMasksToBounds:YES];
//    }
//    
////    navigationController.navigationBar.accessibilityIdentifier = @"navigationbar";
////    self.window.rootViewController = navigationController;
//}

#pragma mark - EMPushManagerDelegateDevice

// 打印收到的apns信息
-(void)didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSError *parseError = nil;
    NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:userInfo
                                                        options:NSJSONWritingPrettyPrinted error:&parseError];
    NSString *str =  [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    
}

@end
